$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'orderCost/erporder/list',
        datatype: "json",
        colModel: [
            {
                label: '操作', name: 'option', width: 60, align: 'center',
                formatter: function (value, options, row) {
                    var actions = [];
                        if(row.commissionStatus==1){
                            actions.push('<a href="javascript:void(0);" onclick="return vm.review(\'' + row.orderNo + '\');">审核</a>&nbsp;&nbsp;|');
                            actions.push('<a href="javascript:void(0);" onclick="return vm.reBackStatus(\'' + row.orderNo + '\');">撤回</a>');
                        }
                        if(row.commissionStatus==2){
                            actions.push('<a href="javascript:void(0);" onclick="return vm.reBackStatus(\'' + row.orderNo + '\');">撤回</a>');
                        }
                        if(row.commissionStatus==0){
                            return '--';
                        }
                        return actions.join('  ');
                }
            },
            { label: '订单号', name: 'orderNo', index: 'order_no', width: 90,align:'center',
                formatter: function (value, options, row) {
                    return '<a href="javascript:;" onclick="return vm.getInfo(\''+ row.orderNo +'\');">'+ row.orderNo +'</a></li>';
                }
            },
            { label: '订单号', name: 'orderNo', index: 'order_no', width: 80, key: true,hidden:true},
            { label: '成本录入状态', name: 'commissionStatus', index: 'commission_status', width: 80,align:'center',
                formatter:function (value) {
                    switch (value){
                        case 0: return '未录入';
                        case 1: return '已录入';
                        case 2: return '已结算';
                    }
                }
			},
            { label: '外协成本', name: 'cost', index: 'cost', width: 60,align:'center',align:'center' },
            { label: '外协商', name: 'info', index: 'info', width: 70,align:'center',align:'center'},
            { label: '物流成本', name: 'logiCost', index: 'logi_cost', width:60,align:'center' },
            { label: '物流公司', name: 'logiCompany', index: 'logi_company', width: 80 },
			{ label: '会员名', name: 'memberName', index: 'member_name', width: 80 },
			{ label: '门店名称', name: 'sellerName', index: 'seller_name', width: 100 ,align:'center'},
			{ label: '原始订单编号', name: 'originalOrderId', index: 'original_order_id', width: 90,align:'center' },
			{ label: '生产状态', name: 'productionStatus', index: 'production_status', width: 80,align:'center',
                formatter:function (value) {
                    switch (value){
                        case 1: return '待审核';
                        case 2: return '待分配';
                        case 3: return '生产中';
                        case 4: return '生产完成';
                        case 5:	return '已发货';
                        case 6: return '已完成';
                        case 7: return '新开单';
                        case 11:return '已作废';
                        default: return "未知状态";
                    }
                }
			},
            { label: '商品名称', name: 'itemName', index: 'item_name', width: 80 },
			{ label: '商品总值', name: 'totalAmount', index: 'total_amount', width: 80 },
			{ label: '订单应付金额', name: 'payableAmount', index: 'payable_amount', width: 80,align:'center' },
			{ label: '订单已支付金额', name: 'payed', index: 'payed', width: 80,align:'center' },
			{ label: '下单时间', name: 'orderCreatetime', index: 'order_createtime', width: 100 },
        ],
		viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
        rowList : [25,50,75],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
            vm.e.producerTypeEnum = userData.producerTypeEnum;
            vm.e.orderProductionStatusEnum = userData.productionStatusEnum;
            vm.e.sellerInfoList = userData.sellerInfoList;
            vm.e.commissionStatusEnum=userData.commissionStatusEnum;
            vm.sumTotal=userData.sumTotal;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		erpOrder: {},
        q: {
            orderNo: '',
            beginCreatetime:'',
            endCreatetime:'',
            sellerId:'',
            productionStatus: '',
            commissionStatus:'',
        },
        e: {
            sellerInfoList:[],
            orderProductionStatusEnum: [],
            commissionStatusEnum:[],
        },
        erpOrderProductionList:[],
        orderItemDetailData:[],
        orderLogisticsList:[],
        sumTotal:{},
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.erpOrder = {};
		},
		update: function (event) {
			var orderNo = getSelectedRow();
			if(orderNo == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(orderNo)
		},
		saveOrUpdate: function (event) {
			var url = vm.erpOrder.orderNo == null ? "orderCost/erporder/save" : "orderCost/erporder/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.erpOrder),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var orderNos = getSelectedRows();
			if(orderNos == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "orderCost/erporder/delete",
                    contentType: "application/json",
				    data: JSON.stringify(orderNos),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(orderNo){
            $.get(baseURL + "orderCost/erporder/info/" + orderNo, function(r){
                vm.erpOrder = r.erpOrder;
                vm.erpOrder.shipMobile=isBlank(vm.erpOrder.shipMobile)?vm.erpOrder.shipTel:vm.erpOrder.shipTel;
                vm.erpOrderProductionList=r.erpOrderProductionList;
                vm.orderLogisticsList=r.orderLogisticsList;
                //格式化数据
                var productionStatus= vm.erpOrder.productionStatus;
                switch (productionStatus){
                    case 1: vm.erpOrder.productionStatus='待审核';break;
                    case 2: vm.erpOrder.productionStatus= '待分配';break;
                    case 3: vm.erpOrder.productionStatus= '生产中';break;
                    case 4: vm.erpOrder.productionStatus= '生产完成';break;
                    case 5:	vm.erpOrder.productionStatus='已发货';break;
                    case 6: vm.erpOrder.productionStatus= '已完成';break;
                    case 7: vm.erpOrder.productionStatus= '新开单' ;break;
                }
                var shipStatus= vm.erpOrder.shipStatus;
                switch (shipStatus){
                    case 0: vm.erpOrder.shipStatus='未发货';break;
                    case 1: vm.erpOrder.shipStatus ='已发货';break;
                    case 2: vm.erpOrder.shipStatus ='部分发货';break;
                }
                var payStatus=vm.erpOrder.payStatus;
                switch (payStatus){
                    case 0: vm.erpOrder.payStatus='未支付';break;
                    case 1: vm.erpOrder.payStatus ='已支付';break;
                    case 3: vm.erpOrder.payStatus ='部分支付';break;
                }
                vm.recordItemData=JSON.parse(JSON.stringify(r.orderItemDetailData));
                vm.orderItemDetailData=r.orderItemDetailData;
                vm.showList = false;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {
                    'orderNo': vm.q.orderNo,
                    'beginCreatetime':vm.q.beginCreatetime,
                    'endCreatetime':vm.q.endCreatetime,
                    'sellerId': vm.q.sellerId,
                    'productionStatus': vm.q.productionStatus,
                    'commissionStatus':vm.q.commissionStatus,
                },
                page:page
            }).trigger("reloadGrid");
		},

        /* 订单起始 */
        checkBeginDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
                    vm.q.beginCreatetime = dp.cal.getNewDateStr();
                }})
        },
        /* 订单结束 */
        checkEndDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
                    vm.q.endCreatetime = dp.cal.getNewDateStr();
                }})
        },
        /* 查询重置 */
        reset: function () {
            vm.q.orderNo = '';
            vm.q.productionStatus = '';
            vm.q.beginCreatetime='';
            vm.q.endCreatetime='';
            vm.q.producerType = '';
            vm.q.sellerId = '';
            vm.q.commissionStatus='';
            vm.reload();
        },

        /* 批量审核 */
        batchAudit: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            this.$confirm('是否确认批量审核, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        var postData = ids.join(',');
                        $.ajax({
                            type: "GET",
                            url: baseURL + "orderCost/erporder/batchAudit?ids="+ postData,
                            contentType: "application/json",
                            data: null,
                            success: function(r){
                                if(r.code === 0){
                                    alert("操作成功!");
                                    vm.reload();
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        /*批量录入*/
        batchEnter:function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            this.$confirm('是否确认批量录入, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        var postData = ids.join(',');
                        $.ajax({
                            type: "GET",
                            url: baseURL + "orderCost/erporder/batchEnter?ids="+ postData,
                            contentType: "application/json",
                            data: null,
                            success: function(r){
                                if(r.code === 0){
                                    alert("操作成功!");
                                    vm.reload();
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        reBackStatus:function (orderNo) {
            this.$confirm('是否确认撤回, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        $.ajax({
                            type: "GET",
                            url: baseURL + "orderCost/erporder/reBackStatus/"+ orderNo,
                            contentType: "application/json",
                            data: null,
                            success: function(r){
                                if(r.code === 0){
                                    alert("操作成功!");
                                    vm.reload();
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        review:function (orderNo) {
            this.$confirm('是否确认审核, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        $.ajax({
                            type: "GET",
                            url: baseURL + "orderCost/erporder/audit/"+ orderNo,
                            contentType: "application/json",
                            data: null,
                            success: function(r){
                                if(r.code === 0){
                                    alert("操作成功!");
                                    vm.reload();
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        //格式化分配情况展示
        formatterStatus:function (row, column, cellValue) {
            if (cellValue == '1') {
                return '待分配'
            } else if (cellValue == '2') {
                return '生产中'
            }else if(cellValue == '3'){
                return '已完成'
            }else if(cellValue == '4'){
                return '已发货'
            }
        },
	}
});